package com.dark.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.PostConstruct;

/**
 * swagger配置
 */
@Slf4j
@Configuration
public class SwaggerConfig {

    @Value("${server.port}")
    private int port;

    Boolean swaggerEnabled = true;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                // 是否开启
                .enable(swaggerEnabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dark.controller"))
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("花落")
                //创建人
                .contact(new Contact("chengluchao", "http://127.0.0.1:5560", "clc@x.com"))
                .version("1.0")
                .description("花落")
                .build();
    }

    /**
     * 启动之后*
     */
    @PostConstruct
    public void applicationAfter() {
        log.info("swagger 访问地址：http://localhost:{}/doc.html", port);
/*        Runnable beep = () -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
               log.error("",e);
            }
            log.info("swagger 访问地址：http://localhost:{}/doc.html", port);
        };
        beep.run();
    }*/
    }
}